<?php

/**
 * Created by PhpStorm.
 * User: WhiteBlue
 * Date: 15/3/4
 * Time: 下午7:47
 */
class userController extends Controller
{
    public function index()
    {
        if (isset($_SESSION['LOGIN'])) {
            $title = site::where('key', '=', 'title')->firstOrFail();
            $content = site::where('key', '=', 'content')->firstOrFail();
            $page = $this->getParam(0, 1);
            $back = content::where('handle', '=', '0')->orderBy('time', 'desc')->forPage($page, 5)->get();
            $this->view = view::make('admin')->with('list', $back)
                ->with('title',$title->value)->with('content',$content->value)->with('page',$page);
        } else {
            $this->renderError('请先登录');
        }
    }

    public function set()
    {
        if ($this->getMethod() == 'POST') {
            $title = site::where('key', '=', 'title')->firstOrFail();
            $title->value = $this->getParam('title', 'null');
            $title->save();

            $content = site::where('key', '=', 'content')->firstOrFail();
            $content->value = $this->getParam('content', 'null');
            $content->save();
        }
        $this->redirect('user/index');
    }

    public function login()
    {
        $pw = site::where('key', '=', 'pw')->firstOrFail();
        if (md5($this->getParam('pw', 0)) == $pw->value) {
            $_SESSION["LOGIN"] = true;
            $this->redirect('user/index');
        } else {
            $this->renderError('密码错误');
        }
    }

    public function logout()
    {
        if (isset($_SESSION['LOGIN'])) {
            session_destroy();
        } else {
            $this->renderError('请先登录');
        }
        $this->redirect('');
    }
}